﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;

namespace dpruna.SharePoint
{
    public partial class ExtendedLookupRelationshipsEditor : UserControl
    {
        #region Fields
        protected SPFieldLookup fldLookup;
        protected InputFormSection inputSectionLookupRelationships;
        protected string m_allowMultiValClientId;
        #endregion

        #region Methods
        internal bool DoesUserHaveRequiredPermissionsOnTargetList(string strLookupList, bool bIsNewField)
        {
            if (this.inputSectionLookupRelationships.Visible && this.IsRIPanelVisible)
            {
                SPRelationshipDeleteBehavior none = SPRelationshipDeleteBehavior.None;
                bool bRIEnforced = false;
                this.GetDeleteBehaviorFromRequestForm(out none, out bRIEnforced);
                if (none != SPRelationshipDeleteBehavior.None)
                {
                    Guid? lookupWebId = null;
                    if (!bIsNewField)
                    {
                        ExtendedLookup fieldFromQueryString = Helpers.GetFieldFromQueryString(this.Page.Request, SPContext.Current.List.Fields) as ExtendedLookup;
                        if (fieldFromQueryString != null)
                        {
                            strLookupList = fieldFromQueryString.LookupList;
                            lookupWebId = new Guid?(fieldFromQueryString.LookupWebId);
                        }
                    }
                    SPList list = ExtendedLookupFieldEditor.GetLookupList(SPControl.GetContextWeb(this.Context), strLookupList, lookupWebId);
                    if (list == null)
                    {
                        throw new SPException(SPResource.GetString("LookupListDeleted", new object[0]));
                    }
                    return list.DoesUserHavePermissions(SPBasePermissions.EmptyMask | SPBasePermissions.ManageLists);
                }
            }
            return true;
        }

        private void GetDeleteBehaviorFromRequestForm(out SPRelationshipDeleteBehavior deleteBehavior, out bool bRIEnforced)
        {
            deleteBehavior = SPRelationshipDeleteBehavior.None;
            bRIEnforced = false;
            string str = base.Request.Form["EnforceRI"];
            string str2 = base.Request.Form["ChooseRelationshipDeleteBehavior"];
            if (!string.IsNullOrEmpty(str) && Helpers.StsCompareStrings(str, "on"))
            {
                bRIEnforced = true;
            }
            if ((bRIEnforced && !string.IsNullOrEmpty(str2)) && (Helpers.StsCompareStrings(str2, "Cascade") || Helpers.StsCompareStrings(str2, "Restrict")))
            {
                deleteBehavior = (SPRelationshipDeleteBehavior)Enum.Parse(typeof(SPRelationshipDeleteBehavior), str2);
            }
        }

        protected void GetDeleteBehaviorInternal(out bool bCascadeDeleteChecked, out bool bRestrictDeleteChecked, out bool bEnforceRI, out bool bIsRelationship)
        {
            if (!this.Page.IsPostBack)
            {
                SPRelationshipDeleteBehavior behavior = (this.fldLookup != null) ? this.fldLookup.RelationshipDeleteBehavior : SPRelationshipDeleteBehavior.None;
                bEnforceRI = behavior != SPRelationshipDeleteBehavior.None;
                bCascadeDeleteChecked = behavior == SPRelationshipDeleteBehavior.Cascade;
                bRestrictDeleteChecked = (behavior == SPRelationshipDeleteBehavior.Restrict) ? true : !bEnforceRI;
                bIsRelationship = (this.fldLookup != null) ? this.fldLookup.IsRelationship : true;
            }
            else
            {
                string str = base.Request.Form["EnforceRI"];
                string str2 = base.Request.Form["ChooseRelationshipDeleteBehavior"];
                bEnforceRI = !string.IsNullOrEmpty(str) && Helpers.StsCompareStrings(str, "on");
                bCascadeDeleteChecked = !string.IsNullOrEmpty(str2) && Helpers.StsCompareStrings(str2, "Cascade");
                bRestrictDeleteChecked = !string.IsNullOrEmpty(str2) && Helpers.StsCompareStrings(str2, "Restrict");
                if (!bEnforceRI)
                {
                    bRestrictDeleteChecked = true;
                }
                string str3 = base.Request.Form["IsRelationship"];
                bIsRelationship = !string.IsNullOrEmpty(str3) && Helpers.StsCompareStrings(str3, "on");
            }
        }

        public void InitializeWithField(ExtendedLookup lookupField, string allowMultiValClientId, bool bTargetListChanged)
        {
            this.fldLookup = lookupField;
            this.m_allowMultiValClientId = allowMultiValClientId;
            if (!this.Page.IsPostBack || bTargetListChanged)
            {
                this.inputSectionLookupRelationships.Visible = true;
                this.IsRIPanelVisible = true;
                this.IsRelationshipCheckboxVisible = true;
                if (this.IsHidden || (SPContext.Current.List == null))
                {
                    this.IsRIPanelVisible = false;
                    this.IsRelationshipCheckboxVisible = false;
                }
                else if (lookupField != null)
                {
                    if (!SPUtility.DoesFieldSupportIsRelationship(lookupField))
                    {
                        this.IsRelationshipCheckboxVisible = false;
                        this.IsRIPanelVisible = false;
                    }
                    if ((lookupField.LookupWebId != SPContext.Current.Web.ID) || lookupField.CountRelated)
                    {
                        this.IsRIPanelVisible = false;
                    }
                    if (lookupField.IsRelationship)
                    {
                        this.IsRelationshipCheckboxVisible = false;
                    }
                }
                else
                {
                    this.IsRelationshipCheckboxVisible = false;
                }
                if (!this.IsRelationshipCheckboxVisible && !this.IsRIPanelVisible)
                {
                    this.inputSectionLookupRelationships.Visible = false;
                }
            }
        }

        internal void OnSaveChange(ExtendedLookup lookupField)
        {
            if (this.inputSectionLookupRelationships.Visible)
            {
                string str = base.Request.Form["IsRelationship"];
                bool flag = true;
                SPRelationshipDeleteBehavior none = SPRelationshipDeleteBehavior.None;
                bool bRIEnforced = false;
                if (this.IsRIPanelVisible)
                {
                    this.GetDeleteBehaviorFromRequestForm(out none, out bRIEnforced);
                    if (bRIEnforced && !lookupField.Indexed)
                    {
                        int num = 0;
                        foreach (SPField field in SPContext.Current.List.Fields)
                        {
                            if (field.Indexed)
                            {
                                num++;
                            }
                        }
                        if (num >= SPFieldIndexCollection.Capacity)
                        {
                            throw new SPException(String.Format("To many indexed fields from field edit. Maximum is {0}", SPFieldIndexCollection.Capacity));
                        }
                        lookupField.Indexed = true;
                    }
                    lookupField.RelationshipDeleteBehavior = none;
                }
                if (this.IsRelationshipCheckboxVisible)
                {
                    if (!string.IsNullOrEmpty(str) && Helpers.StsCompareStrings(str, "on"))
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                    if (none != SPRelationshipDeleteBehavior.None)
                    {
                        flag = true;
                    }
                    lookupField.IsRelationship = flag;
                }
            }
        }
        #endregion

        #region Properties
        internal bool IsHidden
        {
            get
            {
                if (this.ViewState["IsHidden"] == null)
                {
                    return false;
                }
                return (bool)this.ViewState["IsHidden"];
            }
            set
            {
                this.ViewState["IsHidden"] = value;
            }
        }

        protected bool IsRelationshipCheckboxVisible
        {
            get
            {
                if (this.ViewState["IsRelationshipCheckboxVisible"] == null)
                {
                    return false;
                }
                return (bool)this.ViewState["IsRelationshipCheckboxVisible"];
            }
            set
            {
                this.ViewState["IsRelationshipCheckboxVisible"] = value;
            }
        }

        protected bool IsRIPanelVisible
        {
            get
            {
                if (this.ViewState["IsRIPanelVisible"] == null)
                {
                    return false;
                }
                return (bool)this.ViewState["IsRIPanelVisible"];
            }
            set
            {
                this.ViewState["IsRIPanelVisible"] = value;
            }
        }
        #endregion
    }
}
